/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dacnpm;
import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import dacnpm.Form_DangNhap;
import static java.lang.Thread.sleep;
import java.sql.PreparedStatement;

/**
 *
 * @author SONY
 */
public class Form_DangThi extends javax.swing.JFrame {
    Statement stt,stt2;
    Connection conn;
    ResultSet rs;
    ResultSetMetaData metadata;
    DefaultTableModel dfTable;
    String DA=null, tl="sai";

    public static int dem=0;
    public static boolean dung ;
    public static double diem=0.0;
    public static int m=45, s=0;
    public static int i=10;
    public static int j=0;
    //Checkbox dapan_a ,dapan_b,dapan_c,dapan_d;
    ArrayList<CheckboxGroup> arraycbg=new ArrayList<CheckboxGroup>();
    ArrayList<Checkbox> dapan_a=new ArrayList<Checkbox>();
    ArrayList<Checkbox> dapan_b=new ArrayList<Checkbox>();
    ArrayList<Checkbox> dapan_c=new ArrayList<Checkbox>();
    ArrayList<Checkbox> dapan_d=new ArrayList<Checkbox>();
    CheckboxGroup cbg;
     Panel ch, ch2;
     Label lbch,lbstt,lba,lbb,lbc,lbd;
     ArrayList<String> arraycauhoi =  new ArrayList<>();
     ArrayList<String> arraytla = new ArrayList<>();
     ArrayList<String> arraytlb = new ArrayList<String>();
     ArrayList<String> arraytlc = new ArrayList<String>();
     ArrayList<String> arraytld = new ArrayList<String>();
     ArrayList<String> arraytraloi = new ArrayList<String>();
     ArrayList<String> TL = new ArrayList<String>();
     int n=0;
    /**
     * Creates new form Form_DangThi
     */
    public Form_DangThi() throws Exception {
        initComponents();
        lb_KyThi.setText("Cuối Kỳ");
        lb_thoiGian.setText("45 Phút");
        taodethi();
        
        
      Clock cl=new Clock(lb_tgConLai);
        cl.start();
//        while(cl.runable!=false){
//            if(cl.runable==false)
//            {
//                System.out.println("cc");
//            }

    }
   // @Override
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jpn = new javax.swing.JScrollPane();
        pn = new javax.swing.JPanel();
        lb_tgConLai = new javax.swing.JLabel();
        bt_nopbai = new javax.swing.JButton();
        lb_KyThi = new javax.swing.JLabel();
        lb_thoiGian = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 51, 0));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 600));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pn.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout pnLayout = new javax.swing.GroupLayout(pn);
        pn.setLayout(pnLayout);
        pnLayout.setHorizontalGroup(
            pnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 800, Short.MAX_VALUE)
        );
        pnLayout.setVerticalGroup(
            pnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 480, Short.MAX_VALUE)
        );

        jpn.setViewportView(pn);

        jPanel1.add(jpn, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 150, 800, 450));
        jPanel1.add(lb_tgConLai, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 90, 80, 30));

        bt_nopbai.setText("Nop Bai Thi");
        bt_nopbai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_nopbaiActionPerformed(evt);
            }
        });
        jPanel1.add(bt_nopbai, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 80, 220, 50));
        jPanel1.add(lb_KyThi, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 20, 180, 30));
        jPanel1.add(lb_thoiGian, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 50, 180, 30));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/dacnpm/slide4.jpg"))); // NOI18N
        jLabel1.setAutoscrolls(true);
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 800, 600));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bt_nopbaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_nopbaiActionPerformed
        try {
            chamdiem(); 
             
        } catch (Exception ex) {
            Logger.getLogger(Form_DangThi.class.getName()).log(Level.SEVERE, null, ex);
        }
        Form_ThongBaoDiemThi t = new Form_ThongBaoDiemThi();
        this.setVisible(false);
        t.setVisible(true);
        t.setLocation(200, 200);
        this.setEnabled(false);
                
    }//GEN-LAST:event_bt_nopbaiActionPerformed
    public  void LoadDB() throws Exception{
          	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String url ="Jdbc:odbc:user";
		conn = DriverManager.getConnection(url);
		stt=conn.createStatement();
                //dfTable = new DefaultTableModel();
                String sql="Select name_question,question_a,question_b,question_c,question_d,answer  from question1";
                rs = stt.executeQuery(sql);             
                //metadata = rs.getMetaData();
                
    }
   public void taodethi() throws Exception{
                LoadDB();
                
                  
                while (rs.next()){ 
                      //System.out.println("cau hoi" + listcauhoi);                   
                        arraycauhoi.add(rs.getString("name_question"));
                        arraytla.add(rs.getString("question_a"));
                        arraytlb.add(rs.getString("question_b"));
                        arraytlc.add(rs.getString("question_c"));
                        arraytld.add(rs.getString("question_d")); 
                        arraytraloi.add(rs.getString("answer"));
                       n++;
                }
                
                    pn.setLayout(new GridLayout(40, 1));
                    for(int i=0;i<40;i++){
                   arraycbg.add(new CheckboxGroup());
                   ch = new Panel(new GridLayout(5,1));
                   ch2=new Panel(new FlowLayout(FlowLayout.LEFT));
                   lbch = new Label(arraycauhoi.get(i));
                   lbch.setFont(new Font("Times New Roman", Font.BOLD, 14));
                   dapan_a.add(new Checkbox(arraytla.get(i),arraycbg.get(i), false));
                   dapan_b.add(new Checkbox(arraytlb.get(i),arraycbg.get(i), false));
                   dapan_c.add(new Checkbox(arraytlc.get(i),arraycbg.get(i), false));
                   dapan_d.add(new Checkbox(arraytld.get(i),arraycbg.get(i), false));
                   
                   lbstt=new Label("Câu: "+(1+i));
                   ch2.add(lbstt);
                   ch2.add(lbch);
                   ch.add(ch2);
                  // ch.add(lba);
                   ch.add(dapan_a.get(i));
                  // ch.add(lbb);
                   ch.add(dapan_b.get(i));
                   //ch.add(lbc);
                   ch.add(dapan_c.get(i));
                  // ch.add(lbd);
                   ch.add(dapan_d.get(i));
                   pn.add(ch);
                   //jScrollPane1.getViewport().add(pn);
                   DA=arraytraloi.get(i).toString();
                    //System.out.println(DA);
               }

                    
                    
           //jpn=new JScrollPane(pn);
           ///jpn.getViewport().add(pn);
            //add(pn);
   }                  
                      
 public void chamdiem() throws Exception{
     
     //LoadDB();
     
     
     for(int i=0; i<40; i++){
         try{
             dapan_a.get(i).addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent e) {
            // if(e.getStateChange()==ItemEvent.SELECTED) TL="a";
         }
     });
     dapan_b.get(i).addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent e) {
            // if(e.getStateChange()==ItemEvent.SELECTED) TL="b";
         }
     });
     
     dapan_c.get(i).addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent e) {
            // if(e.getStateChange()==ItemEvent.SELECTED) TL="c";
         }
     });
     
     dapan_d.get(i).addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent e) {
             //if(e.getStateChange()==ItemEvent.SELECTED) TL="d";
         }
     });
    if(arraycbg.get(i).getSelectedCheckbox()==dapan_a.get(i)) TL.add("a");
    else if(arraycbg.get(i).getSelectedCheckbox()==dapan_b.get(i)) TL.add("b");
    else if(arraycbg.get(i).getSelectedCheckbox()==dapan_c.get(i)) TL.add("c");
    else if(arraycbg.get(i).getSelectedCheckbox()==dapan_d.get(i)) TL.add("d");
    else TL.add("");
    DA=arraytraloi.get(i);
   // System.out.println(TL.get(i)); 
    tl=TL.get(i);
    if(tl.equals(DA)){
        dem=dem+1;
        diem=diem+0.25;
             }
         }catch (Exception e){}
     }
     //String name= Form_DangNhap.namedb;
     try{
     PreparedStatement stm = conn.prepareStatement("INSERT INTO Diem(name,SoCauDung,Diem) VALUES (?, ?,?)");

                stm.setString(1,Form_DangNhap.name);
                stm.setInt(2, dem);
                stm.setDouble(3, diem);
                
                stm.executeUpdate();
    }catch(Exception e){System.out.println("Lỗi thao tác CSDL");}
 }                  
               
                
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Form_DangThi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Form_DangThi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Form_DangThi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Form_DangThi.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
//        Form_DangThi dangthi = new Form_DangThi();
//        dangthi.setVisible(true);
//        //dangthi.run();
//        if(dung){
//            dangthi.chamdiem();
//            Form_ThongBaoDiemThi t = new Form_ThongBaoDiemThi();
//            dangthi.setVisible(false);
//            t.setVisible(true);
//            t.setLocation(200, 200);
//            dangthi.setEnabled(false);
//        }
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Form_DangThi().setVisible(true);
//                    while(i>0){
//                        lb_tgConLai.setText(m+":"+s);
//                        if(s==0)
//                        if (m>0) {s=59;m--;}
//                        s--;
//                        sleep(1000);
//                        System.out.println(m+":"+s);
//                    }
                } catch (Exception ex) {
                    Logger.getLogger(Form_DangThi.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_nopbai;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jpn;
    private javax.swing.JLabel lb_KyThi;
    public static javax.swing.JLabel lb_tgConLai;
    private javax.swing.JLabel lb_thoiGian;
    private javax.swing.JPanel pn;
    // End of variables declaration//GEN-END:variables
}
